/**
 * @(#) MPAcquisition.java
 *
 * @author lihg
 * @version V1.0 2013-10-6
 *
 * Copyright (C) 2010,2013 , Lihg
 */

/**
 * 
 */
package cn.com.dshome.acquisition.service.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import cn.com.dshome.acquisition.exception.AcquisitionException;
import cn.com.dshome.acquisition.service.Acquisition;
import cn.com.dshome.acquisition.utils.HttpClientUtils;
import cn.com.dshome.acquisition.utils.StringUtils;
import cn.com.dshome.acquisition.vo.BaseInformation;

/**
 * 
 * Purpose:
 * 
 * @author lihg
 * @see
 * @since
 */
@Service("mpAcquisition")
public class MPAcquisition implements Acquisition {

	private static final long serialVersionUID = 3463962807626217703L;

	//记录日志
	private Log log = LogFactory.getLog(MPAcquisition.class) ;

	@Autowired
	@Qualifier("httpClientUtils")
	private HttpClientUtils hcu ;

	private String url = "http://5555mp.com" ;

	@Autowired
	private TaskExecutor taskExecutor ;

	@Override
	public <T extends BaseInformation> List<T> listInformation(Class<T> clazz) throws AcquisitionException {
		fetchMessage() ;
		return null;
	}

	private void fetchMessage() throws AcquisitionException{
		String responseMessage = convertByteToString(hcu.getResponseMessage(url)) ;
		Document document = Jsoup.parse(responseMessage) ;
		//图片区
		List<Element> pictureZone = document.select("table>tbody>tr>td>div>a").subList(0,1) ;
		for(final Element picture : pictureZone){
			taskExecutor.execute(new Runnable() {
				@Override
				public void run() {
					try {
						fetchPictureZone(picture) ;
					} catch (AcquisitionException e) {
						e.printStackTrace();
					}
				}
			}) ;
		}
	}

	private void fetchPictureZone(Element picture) throws AcquisitionException{
		String url = this.url + picture.attr("href") ;
		String responseMessage = convertByteToString(hcu.getResponseMessage(url)) ;
		Document document = Jsoup.parse(responseMessage) ;
		Element element = document.select("div.list li a").get(2) ;
		fetchPictureZoneDetail(element) ;
	}

	private void fetchPictureZoneDetail(Element detail) throws AcquisitionException{
		String url = this.url + detail.attr("href") ;
		String responseMessage = convertByteToString(hcu.getResponseMessage(url)) ;
		Document document = Jsoup.parse(responseMessage) ;
		Elements elements = document.select("div.content img") ;
		for(final Element pc : elements){
			taskExecutor.execute(new Runnable() {
				@Override
				public void run() {
					String pcUrl = pc.attr("src") ;
					log.info("获取图片开始……") ;
					byte[] pcByte;
					try {
						pcByte = hcu.getResponseMessage(pcUrl);
						createFile(pcByte) ;
					} catch (AcquisitionException e) {
						e.printStackTrace();
					}
				}
			}) ;
		}
		fetchPictureZoneDetail(document.select("div.pagea>a").first()) ;
	}

	private void createFile(byte[] byteMsg){
		try {
			File file = new File("D:/pc", new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".jpg");
			if(!file.exists()){
				file.createNewFile() ;
			}
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
			bos.write(byteMsg);
			bos.flush() ;
			bos.close() ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}

	/**
	 * Byte转String
	 * @param content
	 * @return
	 */
	private String convertByteToString(byte[] content){
		try {
			String charSet = hcu.getChartSet() ;
			if (StringUtils.hasLength(charSet)) {
				return StringUtils.byteArray2String(content, charSet);
			} else {
				return StringUtils.byteArray2String(content);
			}
		} catch (Exception e) {
			log.error("convertByteToString failed " ,e) ;
			return null ;
		}
	}

}


/**
 * Revision history
 * -------------------------------------------------------------------------
 * 
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2013-10-6 下午01:20:55 lihg 创建版本
 */